﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using EFB.Lib;
using EFB.Lib.Entity;
using EFB.Lib.Process;

namespace EFB.Web.telasMobile {

    public partial class OperacaoLogistica : System.Web.UI.UserControl {

        public String TipoOperacao { get; set; }
        public String UrlAnterior { get; set; }
        public String Produto { get; set; }

        protected void Page_Load(object sender, EventArgs e) {

            txtOperacao.Text = this.TipoOperacao;
            if (!String.IsNullOrEmpty(Request.QueryString["sku"])) {
                txtOp.Text = Request.QueryString["sku"];
            }

            if (this.TipoOperacao.Equals("Saida", StringComparison.InvariantCultureIgnoreCase))
            {
                txtOp.Enabled = false;
            }

            lblOp.Text = "SKU";
            if (this.TipoOperacao.Equals("Producao", StringComparison.InvariantCultureIgnoreCase)) {
                lblOp.Text = "OP";
            }

        }

        protected void btnLimpar_Click(object sender, EventArgs e) {
            txtRastreabilidade.Text = "";
            txtQuantidade.Text = "";
            txtOp.Text = "";
        }

        protected void Retornar_Click(object sender, EventArgs e) {
            Response.Redirect(this.UrlAnterior, true);
        }

        protected void btnRegistrar_Click(object sender, EventArgs e) {
            BaseProcess Process = new BaseProcess();
            try {

                OperacaoLogEntity OperacaoLog = new OperacaoLogEntity();
                if (!this.TipoOperacao.Equals("Producao", StringComparison.InvariantCultureIgnoreCase))
                {
                    OperacaoLog.Sku = txtOp.Text;               
                }
                else {
                    OperacaoLog.Op = txtOp.Text;
                }
                
                OperacaoLog.Rast = txtRastreabilidade.Text;                
                OperacaoLog.Qtd = txtQuantidade.Text;
                OperacaoLog.Operacao = this.TipoOperacao;

                Process.DatabaseConnect();
                Process.BeginTransaction();

                OperacaoLogProcess OperLogProc = new OperacaoLogProcess();

               
                OperLogProc.Incluir(OperacaoLog);                
                       
                Process.CommitTransaction();

                FailureText.Text = "Operação realizada com sucesso.";
                FailureText.ForeColor = System.Drawing.Color.Blue;
                FailureText.Visible = true;
                FailureContainer.Visible = true;

            } catch (Exception Except) {
                Process.RollbackTransaction();
                FailureText.Text = ContextHelper.FormatExceptionText(Except);
                FailureText.Visible = true;
                FailureContainer.Visible = true;
            } finally {
                Process.DatabaseDisconnect();
            }
        }

    }
}